package com.dk.controller;


import com.alibaba.fastjson.JSONObject;
import com.dk.enums.ResultEnum;
import com.dk.pojo.*;
import com.dk.result.Result;
import com.dk.service.*;
import com.dk.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("sellcars")
@ResponseBody
@Api
@ApiOperation("车辆预约接口")
public class SellcarsController {

   @Autowired
   private SeriesService seriesService;
    @Autowired
    private YearsService yearsService;
    @Autowired
    private ModelsService modelsService;

    @Autowired
    private CityService cityService;

    @Autowired
    private RecordService recordService;
    @Autowired
    private CarService carService;
    @Autowired
    private SubscribeService subscribeService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private JwtUtil jwtUtil;

    //查询全部品牌
    @ApiOperation(value = "查询车辆品牌信息")
    @GetMapping("getbrands")
    public Result getbrands(){
        try {
           List<Tbbrand> list = brandService.getAll();
            return Result.OK().setData(list);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
    //第一级联查 根据品牌id查询系列
    @ApiOperation(value = "根据品牌id查询品牌系列信息")
    @ApiImplicitParam(name = "id", value = "品牌表主键", required = true, paramType = "path")
    @GetMapping("getseries/{id}")
    public Result getTbseriesVO(@PathVariable("id") String id){
        try {
            List<Tbseries> tbseries = seriesService.getseries(id);

            return Result.OK().setData(tbseries);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
    //第二级联查根据系列id查询年份
    @ApiOperation(value = "根据品牌系列id查询车辆年份信息")
    @ApiImplicitParam(name = "id", value = "品牌系列表主键", required = true, paramType = "path")
    @GetMapping("getyears/{seriesId}")
    public Result getYears(@PathVariable("seriesId")String seriesId){
        try{
            List<Tbyears> years = yearsService.getYears(seriesId);
            return Result.OK().setData(years);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
    //第三级联查根据年份id查询型号
    @GetMapping("getmodels/{yearsId}")
    @ApiOperation(value = "根据车辆年份id查询车辆型号信息")
    @ApiImplicitParam(name = "id", value = "车辆年份表主键", required = true, paramType = "path")
    public Result getmodels(@PathVariable("yearsId") String yearsId){

        try {
            List<Tbmodels> models = modelsService.getModels(yearsId);
            return Result.OK().setData(models);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
    //获取上牌时间和行驶里程
    @ApiOperation(value = "根据车辆id查询上牌时间和行驶里程")
    @ApiImplicitParam(name = "id", value = "车辆表主键", required = true, paramType = "path")
    @GetMapping("getnumtime/{cid}")
    public Result  getNumTime(@PathVariable("cid") Long cid){
        try {
            Dicrecord dicrecord = recordService.getNumTime(cid);
            return Result.OK().setData(dicrecord);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
    //获取价格
    @GetMapping("getprice/{modelsId}")
    @ApiOperation(value = "根据车辆型号id查询价格")
    @ApiImplicitParam(name = "id", value = "车辆型号表主键", required = true, paramType = "path")
    public Result getprice(@PathVariable("modelsId")String modelsId){
        try {
            Tbcar car = carService.getprice(modelsId);
            return Result.OK().setData(car);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
    /*获取当前ip所在地*/
    @GetMapping("getip")
    @ApiOperation(value = "获取当前所在地")
    public String getAddress() {
        String s = "";
        CloseableHttpClient client = HttpClients.createDefault();
        //创建链接
        HttpGet httpGet = new HttpGet("https://www.mxnzp.com/api/ip/self?app_id=s3vjdrfvrseqqgtn&app_secret=Kzgzd1V3czZPQ3FORjNheXZuTk4xZz09");
        CloseableHttpResponse response=null;
        try {
            //响应
            response = client.execute(httpGet);
            //主体
            HttpEntity entity = response.getEntity();
            s = EntityUtils.toString(entity,"utf-8");
            JSONObject jsonObject = JSONObject.parseObject(s);
            Object data = jsonObject.get("data");
            String s1 = JSONObject.toJSONString(data);
            JSONObject jsonObject1 = JSONObject.parseObject(s1);
            Object peovince = jsonObject1.get("city");
            s=peovince.toString();
            //确保被关闭
            EntityUtils.consume(entity);
            //响应主体
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (client !=null){
                try {
                    client.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (response != null){
                try {
                    response.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return s;
    }

    //通过id查询选中的车型
    @GetMapping("getmodel/{modelsId}")
    @ApiOperation(value = "根据车辆型号id获取到所选车型")
    @ApiImplicitParam(name = "id", value = "车辆型号表主键", required = true, paramType = "path")
    public Result getmodel(@PathVariable("modelsId")String modelsId){
        try {
            //通过id获取到选中的车型
            Tbmodels s = modelsService.getModel(modelsId);
            return Result.OK().setData(s);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }

    }
    //预约功能
    @PostMapping("subscribe/{modelsId}")
    @ApiOperation(value = "所选车型id添加预约表")
    @ApiImplicitParam(name = "id", value = "车辆型号表主键", required = true, paramType = "path")
    public Result subscribe(@PathVariable("modelsId") String modelsId,@RequestHeader("AuthToken")String token){
        try {
            //获取选中车型
            Result result = getmodel(modelsId);
            Tbmodels data = (Tbmodels) result.getData();
            String modelsName = data.getModelsName();
            Tbsubscribe sub = new Tbsubscribe();
            sub.setName(modelsName);
            //获取地址
            String address = getAddress();
            sub.setAddress(address);
            //获取token中的联系方式
            Claims claims = jwtUtil.parseJwt(token);
            Tbsubscribe tbsubscribe = JSONObject.parseObject(claims.getSubject(), Tbsubscribe.class);
            String phone = tbsubscribe.getPhone();
            sub.setPhone(phone);
            sub.setTime("待客服联系");
            sub.setStatus("0");
            //预约信息添加到预约表中
            subscribeService.subacribe(sub);
            return Result.respones(ResultEnum.SUBSCRIBE_OK);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }

    //预约审核 查询未审核和审核失败的车辆预约信息
    @GetMapping("getnopass")
    @ApiOperation(value = "查询预约表中未审核和审核失败的车辆信息")
    public Result getnopass(){
        try {
            List<Tbsubscribe> sub = subscribeService.getnopass();
            return Result.OK().setData(sub);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
    //审核通过
    @PutMapping("pass/{id}")
    @ApiOperation(value = "所选车型审核通过")
    @ApiImplicitParam(name = "id", value = "车辆型号表主键", required = true, paramType = "path")
    public Result pass(@PathVariable("id") Long id){
        try {
            subscribeService.pass(id);
            subscribeOK();
            return Result.OK();
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
    //审核未通过
    @ApiOperation(value = "所选车型审核不通过")
    @ApiImplicitParam(name = "id", value = "车辆型号表主键", required = true, paramType = "path")
    @PutMapping("nopass")
    public Result nopass(Long id){
        try {
            subscribeService.nopass(id);
            return Result.OK();
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
    //审核通过后添加到预约到店管理表中
    @PostMapping("subscribeOK")
    @ApiOperation(value = "审核通过后添加到预约到店管理表中")
    public Result subscribeOK(){
        try {
            subscribeService.subscribeOK();
            return Result.OK();
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
}
